package ru.spb.osll.myfit.common;

import java.util.ArrayList;
import java.util.List;

import ru.spb.osll.myfit.common.util.Pair;

public class TextParsingTools {
  
  public static final String DATA_FILE_NAME = "content.txt";
  
  public static final String PATH = "!path ";
  
  public static final String ADDRESS_BAR_SEPARATOR_REGEX = "\\.";

  public static final String ADDRESS_BAR_SEPARATOR = ".";
  
  public static final String TABLE_CELL_SEPARATOR = "|";
  
  public static final String PATH_SEPARATOR = ";";
  
  public static final String CR_and_NEL = "\r\n";
  
  public static final String PAGE_PATH_ATTRIBUTE = "pagePath";
  
  public static final String URI_SEPARATOR = "/";
  
  public static final String INCLUDE = "!include ";
  
  public static final String CONTENT = "!contents ";
  
  public static final String RECURSIVE_CALL = "Recursive call of the page: ";
  
  public static final String LOADING_EXCEPTION = "Error loading page: ";
  
  public static final String SETUP = "SetUp";
  
  public static final String TEARDOWN = "TearDown";
  
  public static final String METADATA_FILE_NAME = "metadata.properties";
  
  public static final String TAGS_PROPERTY_NAME = "myfit_page_tags";
  
  /**
   * Content of header import table
   */
  public static final String IMPORT_HEADER = "Import";
  
  
  public static String getPostfix(String str, String separator) {
    int index = str.lastIndexOf(separator);
    
    return str.substring(index+1, str.length()); 
  }
  
  public static List<Pair<String, String>> getPagePathes(String pagePath){
    return createPagePaths(splitPagePath(pagePath));
  }
  
  public static String[] splitPagePath(String pagePath){
    ArrayList<String> list = new ArrayList<String>();
    String[] names = pagePath.split(ADDRESS_BAR_SEPARATOR_REGEX);
    
    for(String name: names){
      if(name.equals("")){
        continue;
      }
      list.add(name);
    }
    return list.toArray(new String[list.size()]);
  }
  
  public static List<Pair<String, String>> createPagePaths(String[] pagenames){
    ArrayList<Pair<String, String>> pagePaths = new ArrayList<Pair<String,String>>();
    if(pagenames.length == 0 ){
      return pagePaths;
    }
    
    int size = pagenames.length - 1;
    
    String parent = ""; 
    for(int i = 0; i < size; i++){
      String name = pagenames[i];
      parent += name;
      pagePaths.add(new Pair<String, String>(name, parent));
      parent += ADDRESS_BAR_SEPARATOR;
    }
    
    String name = pagenames[size];
    parent += name ;
    pagePaths.add(new Pair<String, String>(name, parent));
    return pagePaths;
  } 
}
